﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HanGu.Services.Interface;
using HanGu.Entity;

namespace HanGu.Services
{
    public class DictionaryService : BaseService, IDictionary
    {

        public List<Dictionary> GetAll()
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.Dictionary.ToList();
            }
        }

        public List<Dictionary> GetByGroupName(string groupName)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.Dictionary.Where(c => c.GroupName == groupName).ToList();
            }
        }

        public List<Dictionary> GetAll(UniversallySearchParam searchparma)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var query = obj.Dictionary.Where(c => 1 == 1);
                if (!string.IsNullOrEmpty(searchparma.Ttile))
                {
                    query = query.Where(c => c.Title.IndexOf(searchparma.Ttile) >= 0);
                }
                if (!string.IsNullOrEmpty(searchparma.Lectcher))
                {
                    query = query.Where(c => c.key.IndexOf(searchparma.Lectcher) >= 0);
                }
                query = Paging<Dictionary,string >(c=>c.GroupName,query, searchparma.Pagination);
                return query.ToList();
            }
        }

        public Dictionary GetByID(string key)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.Dictionary.Where(c => c.GenerateKey == key).FirstOrDefault();
            }
        }

        public Dictionary GetByID(string key, string groupname)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.Dictionary.Where(c => c.GroupName == groupname && c.key == key).FirstOrDefault();
            }
        }


        public bool Add(Dictionary model)
        {
            try
            {
                if (model.key == "-99")
                {
                    using (HaiGuEntities obj = new HaiGuEntities())
                    {
                        var result = obj.spCreateDictionary(model.GroupName, model.Value, model.Title);
                    }
                }
                else
                {
                    this.Add<Dictionary>(model);
                }
                return true;
            }
            catch { return false; }
        }
        public int? AddBySP(Dictionary model)
        {

            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var result = obj.spCreateDictionary(model.GroupName, model.Value, model.Title);
                return result.FirstOrDefault();
            }

        }
        public bool Update(Dictionary model)
        {
            try
            {
                this.Update<Dictionary>(model);
                return true;
            }
            catch { return false; }
        }

        public bool Delete(string key, string groupName)
        {
            try
            {
                this.Delete<Dictionary>(c => c.GenerateKey == key && c.GroupName == groupName);
                return true;
            }
            catch { return false; }
        }

        public bool DeleteGroupName(string groupName)
        {
            try
            {
                this.Delete<Dictionary>(c => c.GroupName == groupName);
                return true;
            }
            catch { return false; }
        }

        public bool IsExits(string groupName, string key)
        {
            bool isExit = false;
            try
            {
                using (HaiGuEntities obj = new HaiGuEntities())
                {
                    isExit = obj.Dictionary.Where(c => c.GroupName == groupName && c.key == key).Count() > 0;
                }
            }
            catch
            { }

            return isExit;
        }


    }
}
